<?php
/**
 * 极光推送
 */

namespace App\exption\push\Jpush;

use App\interf\Push;
use App\exption\push\Jpush\src\Client;

class Jpush implements Push
{

    protected $app_key;
    protected $app_secret;
    protected $appPkgName='';
    protected $client;

    public function __construct()
    {
        $this->app_key = config('push')['jpush']['app_key'];
        $this->app_secret = config('push')['jpush']['app_secret'];
        $this->client = new Client($this->app_key, $this->app_secret);
    }

    /*
     * 单个推送
     * param array $params = [
     * 'title'=>必填
     * 'content'=>必填
     * 'description'=>必填
     * 'mobile_list'=>必填
     * 'bi_tag'=>'n'
     * 'icon'=>'n']  // 消息内容参数
     * param array $mobile_list       移动设备号列表   * 单个推送
     * */
    public function  singlePush($params)
    {
        if( !is_array($params) || !array_key_exists('title', $params) || !array_key_exists('content', $params) || !array_key_exists('description', $params) ||
            !array_key_exists('mobile_list', $params))
            return ['status'=>'error', 'message_code'=>10128, 'message'=>'参数异常', 'data'=>$params];

        $temp_arr = [];
        $temp_arr['type'] = $params['notify_id'];
        if(isset($params['extend']) && !empty($params['extend']))
            foreach ($params['extend'] as $key=>$value)
                $temp_arr[$key] = $value;

        // 透传
        if(!isset($params['msg_type']) || $params['msg_type'] == config('params.push_type.ospf'))
            return $this->SendPushMessage($params['mobile_list'], $params['content'], $temp_arr);
        // 通知
        else if(isset($params['msg_type']) || $params['msg_type'] == config('params.push_type.inform'))
            return $this->alert($params['mobile_list'], $params['content']);
    }

    /*
     * 批量推送   --暂时不用
     * */
    public function batchPush($params)
    {
        /*if( !is_array($params) || !array_key_exists('title', $params) || !array_key_exists('content', $params) || !array_key_exists('description', $params) ||
            !array_key_exists('mobile_list', $params) || !is_array($params['mobile_list']))
            return ['status'=>'error', 'message'=>'参数异常', 'data'=>$params];
        if(count($params['mobile_list']) > 1)
            return ['status'=>'error', 'message'=>'请选择批量发送', 'data'=>$params];

        // 透传
        if(!isset($params['notify_foreground']) || $params['notify_foreground'] == config('params.push_type.ospf'))
            return $this->SendPushMessage(implode(',', $params['mobile_list']), $params['content'], [$params['notify_id']]);
        // 通知
        else if(isset($params['notify_foreground']) || $params['notify_foreground'] == config('params.push_type.inform'))
            return $this->alert(implode(',', $params['mobile_list']), $params['content']);*/
    }

    /*
     * 推送消息-透传
     * $registration_id 设备id  批量发送用，逗号隔开
     * $content 发送内容
     * $extra  推送扩展   --自定义扩展
     * */
    protected function SendPushMessage($registration_id = '', $content = '', $extra = [])
    {
        if ($registration_id) {
            $msg = [];
            if (!empty($extra))
                $msg['extras'] = $extra;

            try {
                $pusher = $this->client->push();
                $response = $pusher//->setCid($cid)
                ->setPlatform('all')            // all：所有设备， android:安卓， ios：苹果，winphone：不懂-哈哈
                ->addRegistrationId($registration_id)
                    ->message($content, $msg)
                    ->send();
                // 推送成功
                if(isset($response['http_code']) && $response['http_code'] == '200' )
                    return ['status'=>'success', 'message_code'=>10130, 'message'=>'推送成功', 'errorCode'=>'0', 'push_response'=>$response];

                return ['status'=>'error', 'message_code'=>10131, 'message'=>$response['body'] ?? '推送消息异常', 'errorCode'=>$response['http_code'] ?? '未知状态码', 'push_response'=>$response];
            } catch (\JPush\Exceptions\JPushException $e) {
                return ['status'=>'error', 'message_code'=>10131, 'message'=>$e->getMessage(), 'errorCode'=>'未知状态码', 'push_response'=>''];
            }
        }
        return ['status'=>'error', 'message_code'=>10133, 'message'=>'未知推送设备', 'errorCode'=>'未知状态码', 'push_response'=>''];
    }

    /*
     * @param string $registration_id
     * @param string $alert
     * 系统通知
     * */
    public function alert($registration_id = '', $alert = '')
    {
        if ($registration_id) {
            //$cid = md5(microtime() . $registration_id);
            try {
                $pusher = $this->client->push();
                $response = $pusher//->setCid($cid)
                ->setPlatform('all')
                    ->addRegistrationId($registration_id)
                    ->setNotificationAlert($alert)
                    ->send();

                // 推送成功
                if(isset($response['http_code']) && $response['http_code'] == '200' )
                    return ['status'=>'success', 'message_code'=>10130, 'message'=>'推送成功', 'errorCode'=>'0', 'push_response'=>$response];

                return ['status'=>'error', 'message_code'=>10131, 'message'=>$response['body'] ?? '推送消息异常', 'errorCode'=>$response['http_code'] ?? '未知状态码', 'push_response'=>$response];
            } catch (\JPush\Exceptions\JPushException $e) {
                return ['status'=>'error', 'message_code'=>10131, 'message'=>$e->getMessage(), 'errorCode'=>'未知状态码', 'push_response'=>''];
            }
        }
        return ['status'=>'error', 'message_code'=>10133, 'message'=>'未知推送设备', 'errorCode'=>'未知状态码', 'push_response'=>''];
    }

}